import pandas as pd
import copy
from sklearn.svm import SVR, LinearSVR
import warnings
from sklearn.model_selection import KFold
import matplotlib.pyplot as plt
from sklearn.ensemble import GradientBoostingRegressor, RandomForestRegressor
import numpy as np
warnings.filterwarnings('ignore')

version = ''
all_label = pd.read_csv('data/men_{version}all_label.csv'.format(version=version), header=0)
all_fea_month = pd.read_csv('data/men_{version}all_fea_month.csv'.format(version=version), header=0)
all_fea_month_tst = pd.read_csv('data/men_{version}all_fea_month_tst2.csv'.format(version=version), header=0)
men_pred = pd.read_csv('data/men_wan.csv', header=None)
men_pred.set_index(0, inplace=True)
header = pd.read_csv('data/men_{version}all_fea_header.csv'.format(version=version), header=0, index_col=0)


X_trn = all_fea_month
X_tst = all_fea_month_tst

std = all_fea_month.std(axis=0)
cols = std.sort_values(ascending=False).index.to_list()
# cols = list(map(str, range(std.shape[0])))

kf = KFold(n_splits=6, shuffle=True)  # 此时只随机一次，伪随机
model = SVR()
# model = LinearSVR(C=10)
# 最大深度为2，50时达到最trn.py低值；最大深度不限制120个达到最低值
# 调参: 0.186, 不调参，最后80个特征以后收敛到0.4
model = GradientBoostingRegressor(n_estimators=20, min_samples_split=3, min_samples_leaf=19, max_depth=2)
# model = GradientBoostingRegressor()
# model = RandomForestRegressor()


R = []
for ii in range(10):
    x1 = []
    y1 = []
    y2 = []
    ind_all = list(range(X_trn.shape[0]))
    # ind_2017 = list(range(4*81, 5*81))
    ind_2017 = []
    indices = [a for a in ind_all if a not in ind_2017]
    X0 = X_trn.ix[indices, :].reset_index().drop('index', axis=1)
    y = all_label.ix[indices].reset_index().drop('index', axis=1)
    global_bstacc = float(np.inf)
    bst_acc = float(np.inf)
    for i in range(40, 250, 1):
        Acc = []
        Acc_trn = []
        X = X0.ix[:, cols[:i]]
        x1 += [i]
        for train_index, test_index in kf.split(X):
            X_train, y_train = X.ix[train_index, :], y.ix[train_index, :]
            X_test, y_test = X.ix[test_index, :], y.ix[test_index, :]

            model.fit(X_train, y_train)
            pred_trn = model.predict(X_train)
            pred = model.predict(X_test)
            acc_trn = sum((pred_trn - y_train.values.T[0]) ** 2) / 2 / len(pred_trn)
            acc = sum((pred - y_test.values.T[0]) ** 2) / 2 / len(pred)
            Acc += [acc]
            Acc_trn += [acc_trn]
            if acc < global_bstacc:
                global_bstacc = acc
                model_global_bst = copy.deepcopy(model)
                glo_i = i
        # print(acc)
        if np.mean(Acc) < bst_acc:
            bst_acc = np.mean(Acc)
            model_bst = copy.deepcopy(model)
            bst_i = i

        print(i, np.mean(Acc), bst_acc)
        y1 += [np.mean(Acc_trn)]
        y2 += [np.mean(Acc)]
    plt.plot(x1, y2)
    plt.show()


    men_pred.columns = [0]

    tstX = all_fea_month_tst.ix[:, cols[:bst_i]]
    pred = model_bst.predict(tstX)
    pred_df = pd.DataFrame(pred)
    pred_df.index = men_pred.index
    result = men_pred.ix[:, 0] + pred_df.ix[:, 0]
    result.to_csv('res/19-no-global-gbdt_no.csv', header=False, index=True)
    R += [result]

R1 = pd.concat(R, axis=1)
R1men = R1.mean(axis=1)
R1men.to_csv('res/19-no-global_engbdt_men.csv', header=False, index=True)
# result.to_csv('res/18-no-global.csv', header=False, index=True)
#
# tstX = all_fea_month_tst.ix[:, cols[:bst_i]]
# pred = model_bst.predict(tstX)
# pred_df = pd.DataFrame(pred)
# pred_df.index = men_pred.index
# result = men_pred.ix[:, 0] + pred_df.ix[:, 0]
# result.to_csv('res/18-no-bst.csv', header=False, index=True)


# pred2017 = pd.read_csv('res/1-res0912.csv', header=None, index_col=0)
# pred2017.columns = [0]
# men_pred.columns = [0]
# pred2017_delta = all_label.ix[ind_2017].reset_index().drop('index', axis=1)
# pred2017_delta.index = men_pred.index
#
# tstX = all_fea_month.ix[ind_2017, cols[:bst_i]].reset_index().drop('index', axis=1)
# pred = model_bst.predict(tstX)
# pred_df = pd.DataFrame(pred)
# pred_df.index = pred2017.index
# result1 = pred2017.ix[:, 0] - (pred_df.ix[:, 0] + men_pred.ix[:, 0])
# result2 = pred2017_delta.ix[:, 0] - pred_df.ix[:, 0]
# # print(sum(result1.values**2)/2/len(result1.values))
# print(sum(result2.values**2)/2/len(result2.values))
#
# tstX = all_fea_month.ix[ind_2017, cols[:glo_i]].reset_index().drop('index', axis=1)
# pred = model_global_bst.predict(tstX)
# pred_df = pd.DataFrame(pred)
# pred_df.index = pred2017.index
# result1 = pred2017.ix[:, 0] - (pred_df.ix[:, 0] + men_pred.ix[:, 0])
# result2 = pred2017_delta.ix[:, 0] - pred_df.ix[:, 0]
# # print(sum(result1.values**2)/2/len(result1.values))
# print(sum(result2.values**2)/2/len(result2.values))



